package com.moment.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.moment.model.UserReg;
import com.moment.model.WalletData;
import com.moment.util.Converter;
import com.moment.webservice.Services;
import com.moment.webservice.ServicesProxy;

/**
 * Servlet implementation class LogoutController
 */
@WebServlet("/LogoutController")
public class LogoutController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogoutController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doLogout(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doLogout(request, response);
	}
	protected void doLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession(false);
		System.out.println("value of user in servlet ===>"+session.getAttribute("username"));
		String mobilenumber = (String) session.getAttribute("username");//request.getParameter(session.getAttribute("username"));
		System.out.println("MobileNumber"+mobilenumber);
		//--New CODE-------------
		Map<String,String> map=new HashMap<String,String>();
		map.put("method", "99");
		map.put("mnum", mobilenumber);
		
		String json=new Converter().mapToJson(map);
		System.out.println("json String :"+json);
		Services serviceproxy=new ServicesProxy();
		
		String ss=serviceproxy.service(json);
		//---------------------------
		
//			WebsProxy proxy = new WebsProxy();
//			String ss = proxy.logout(mobilenumber);
			System.out.println("Data From webservices" + ss);
		
			
				/*RequestDispatcher rd = getServletContext()
						.getRequestDispatcher("/MMP/jsp/index.jsp?error=null");
				rd.include(request, response);*/
				//HttpSession session = request.getSession(false);
		/*		 System.out.println("User="+session.getAttribute("username"));
			        if(session != null){
			        	System.out.println("session NUll");
			        	session.setAttribute("username", null);
			            session.invalidate();
			        }
			    	response.sendRedirect("/MMP/jsp/index.jsp");*/
			  //  request.getRequestDispatcher("jsp/index.jsp").forward(request, response);  
			   

		
		
		/*
		 * new code for logout
		 */
		response.setContentType("text/html");
    	Cookie[] cookies = request.getCookies();
    	if(cookies != null){
    	for(Cookie cookie : cookies){
    		if(cookie.getName().equals("JSESSIONID")){
    			System.out.println("JSESSIONID="+cookie.getValue());
    		}
    		 cookie.setMaxAge(0);
    	     response.addCookie(cookie);
			

    	}
    	}
    	//invalidate the session if exists
    	//HttpSession session = request.getSession(false);
    	System.out.println("username="+session.getAttribute("username"));
    	if(session != null){

    		session.setAttribute("username", null);
    		session.invalidate();
    	}
    	response.sendRedirect("jsp/index.jsp");
    }
	

}
